<template>
  <div class="app-foundation">
    <div class="app-foundation-container">
      <aside v-show="false" class="foundation-main-side">
        <div class="app-nav">
          <div v-for="menu in menuList" :key="menu.name"
            :class="{'router-active': menuActive.indexOf(menu.path) === 0 }"
            @click="menuChangeRouter(menu)"
            :title="menu.title"
            class="nav-a-btn">
            <span v-if="menu.name==='home'">▥</span>
            <span v-else>▥</span>
          </div>
        </div>
      </aside>
      <main class="foundation-main-content">
        <div class="foundation-app-content">
          <router-view></router-view>
          <!-- 微应用容器 -->
          <div id="apps"></div>
        </div>
      </main>
    </div>
  </div>
</template>

<script setup>
import { reactive, toRefs, ref } from '@vue/reactivity'
import { computed, watch, onMounted } from '@vue/runtime-core'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { actions, apps } from '@/micros'
import dhelper from '@/components/dhelper/dhelper.js'
dhelper.init({
  nodes: [
    { text: '主页', url: 'http://www.daelui.com' },
    { text: '联系', url: 'http://www.daelui.com/#/about' },
    { text: '博客', url: 'http://www.daelui.com/dblog/index.html' },
    { text: '关闭', action: 'close' }
  ]
})

const Router = useRouter()
const Route = useRoute()
const Store = useStore()

// 子应用列表
const micros = apps.map(item => {
  return {
    name: item.name,
    path: '/' + item.name,
    title: item.title
  }
})

const state = reactive({
  menuList: [
    {
      name: 'home',
      path: '/home',
      title: '首页'
    },
    ...micros
  ],
  menuActive: computed(() => Route.path),
  crumbsRouter: computed(() => {
    let name = ''
    state.menuList.forEach(item => {
      if (state.menuActive == item.path) {
        name = item.title
      }
    })
    return name
  }),
  // 从环境变量中取参数
  appId: process.env.VUE_APP_MICRO_ENTRY,
  appToken: computed(() => Store.state.token)
})

watch(() => Route.path, (val, oval) => {
  console.log('监听路由变化', val, oval)
  let title = '前端技术记录'
  apps.forEach(item => {
    if (val.indexOf(item.name) === 1) {
      title = item.title
    }
  })
  document.title = title
})

onMounted(() => {
  actions.onGlobalStateChange((state) => {
    // state: 变更后的状态; prevState: 变更前的状态
    console.log('主应用观察者：状态改变', state);
    let token = state.globalToken
    Store.commit('setToken', token)
  })
  let lm = document.querySelector('.d-loading-maker')
  lm && (lm.style.display = 'none')
})

const menuChangeRouter = (row) => {
  // state.menuActive = row.name
  // 路由跳转方式
  Router.push({ path: row.path })
  // 跳转方法二 
  //  window.history.pushState({}, '', '/#'+row.path)
}

const loginOut = () => {
  Store.commit('loginOut')
  Router.push('/login')
}

const { menuList, menuActive, crumbsRouter, appId, appToken } = toRefs(state)
</script>

<style lang="scss">
html,body {
  margin: 0;
  padding: 0;
}
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}
$leftWidth: 200px;
.app-foundation {
  display: flex;
  justify-content: space-between;
  width: 100%;
  height: 100vh;
}
.app-foundation-container {
  display: flex;
  flex-direction: row;
  flex: 1;
  flex-basis: auto;
  box-sizing: border-box;
  min-width: 0;
}
.foundation-main-side {
  overflow: auto;
  box-sizing: border-box;
  flex-shrink: 0;
  width: 40px;
}
.foundation-main-content {
  display: block;
  flex: 1;
  flex-basis: auto;
  overflow: auto;
  padding: 0;
}
.app-nav {
  height: 100%;
  display: flex;
  flex-direction: column;
  box-shadow: 2px 0px 10px 0px rgb(0, 47, 60, 0.2);
  padding: 4px;
  box-sizing: border-box;
  background: #fff;
  z-index: 9;
  text-align: center;

  .nav-a-btn {
    padding: 4px;
    background: #f3f4f5;
    margin-bottom: 10px;
    font-weight: bold;
    cursor: pointer;
  }

  .router-active {
    color: #3391ff;
    background: #deeefdde;
  }
}
.foundation-app-content {
  flex: 1;
  height: 100%;
  .app-header-content {
    padding: 0 20px;
    width: 100%;
    height: 50px;
    background: #ffffff;
    box-shadow: 0px 0px 8px 0px rgb(0 0 0 / 8%);
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    border-bottom: 1px solid #ccc;
  }
  .app-container {
    width: 100%;
    height: calc(100% - 50px);
    overflow: auto;
    box-sizing: border-box;
  }
}

/* 修改滚动条默认样式 */
::-webkit-scrollbar {
  width: 8px;
  background: white;
}

::-webkit-scrollbar-corner,
::-webkit-scrollbar-thumb,
::-webkit-scrollbar-track {
  border-radius: 4px;
}

@media(max-width: 756px) {
  ::-webkit-scrollbar {
    display: none!important;
    width: 0px;  
    height: 0px;  
  }
}
</style>
